package com.itqian.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itqian.system.entity.RoleAudit;
import com.itqian.system.entity.User;
import com.itqian.system.mapper.RoleAuditMapper;
import com.itqian.system.mapper.UserMapper;
import com.itqian.system.service.RoleAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itqian.system.utils.RoleUtil;
import com.itqian.system.vo.RoleAuditVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 身份认证审核表 服务实现类
 * </p>
 *
 * @author shishuqian
 * @since 2022-03-21
 */
@Service
public class RoleAuditServiceImpl extends ServiceImpl<RoleAuditMapper, RoleAudit> implements RoleAuditService {

    @Autowired
    private RoleAuditMapper roleAuditMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public Map<String, Object> getAudit(Integer page, Integer size, String type) {
        Page<RoleAudit> auditPage = new Page<>(page, size);
        QueryWrapper<RoleAudit> wrapper = new QueryWrapper<>();

        if (type.equals("wait")){  //查询未审核的
            wrapper.eq("audit_status",0);
        }else if (type.equals("passed")){  //查询审核通过的
            wrapper.eq("audit_status",1);
        }else {  //查询审核未通过的
            wrapper.eq("audit_status",2);
        }
        wrapper.orderByDesc("update_time");
        Page<RoleAudit> roleAuditPage = roleAuditMapper.selectPage(auditPage, wrapper);

        List<RoleAudit> records = roleAuditPage.getRecords();
        List<RoleAuditVO> list = new ArrayList<>();
        for (RoleAudit record : records) {
            RoleAuditVO vo = new RoleAuditVO();
            BeanUtils.copyProperties(record,vo);
            vo.setCf(Arrays.asList(record.getIdCardFront()));
            vo.setCb(Arrays.asList(record.getIdCardBack()));
            vo.setBl(Arrays.asList(record.getBusinessLicense()));
            list.add(vo);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("content",list);
        map.put("size",roleAuditPage.getSize());
        map.put("total",roleAuditPage.getTotal());
        return map;
    }

    @Override
    public boolean pass(Integer id, String type) {

            RoleAudit roleAudit = roleAuditMapper.selectById(id);
            roleAudit.setAuditStatus(1);
            int i1 = roleAuditMapper.updateById(roleAudit);
            //查询进行申请的用户
            User user = userMapper.selectById(roleAudit.getUserId());
            //获取用户当前身份
            String role = user.getRole();
            //获取用户添加新身份后的role字符串
            String roleStr = RoleUtil.addRole(role, type);
            //设置用户身份,更新到数据库
            user.setRole(roleStr);
            int i = userMapper.updateById(user);

            return i==1;
    }
}
